<template>
  <view class="warp">
    <view class="item" v-for="(item, index) in siteList" :key="item.address_id" @click="selectAddress(item)">
      <view class="top">
        <view class="name">{{ item.consignee }}</view>
        <view class="phone">{{ item.mobile }}</view>
        <view class="tag" v-if="item.is_default"><text class="red">默认</text></view>
      </view>
      <view class="bottom">
        {{ item.region }} {{ item.address }}
        <view class="icon-box">
          <view @tap.stop="edit(item.address_id)"><u-icon name="edit-pen" :size="40" color="#999999"></u-icon></view>
          <view @tap.stop="deleteItem(item.address_id, index)"><u-icon name="trash" color="#999999" :size="40" class="delete"></u-icon></view>
        </view>
      </view>
    </view>
    <view class="addSite" @tap="add">
      <view class="add">
        <u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>
        新建收货地址
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      siteList: [],
      type: ''
    };
  },
  onLoad(options) {
    this.type = options.type;
    if (this.type == 'select') {
      uni.setNavigationBarTitle({
        title: '选择地址'
      });
    }
  },
  onShow() {
    this.getData();
  },
  methods: {
    selectAddress(item) {
      if (this.type == 'select') {
        this.$store.commit('storeAddress', item);
        uni.navigateBack();
      }
    },
    getData() {
      this.$api.get({
        url: 'portal/my/get_address_list',
        success: res => {
          if (res.code == 1) {
            this.siteList = res.data;
          } else {
            this.$toast(res.msg);
          }
        }
      });
    },
    deleteItem(id, index) {
      let that = this;
      uni.showModal({
        title: '提示',
        content: '确定要删除该地址吗？',
        success: res => {
          if (res.confirm) {
            that.$api.post({
              url: 'portal/my/delete_address',
              data: {
                address_id: id
              },
              success: res => {
                that.$toast(res.msg);
                if (res.code == 1) {
                  that.siteList.splice(index, 1);
                }
              }
            });
          }
        }
      });
    },
    add() {
      uni.navigateTo({
        url: './log?address_id=0'
      });
    },
    edit(id) {
      uni.navigateTo({
        url: './log?address_id=' + id
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.warp {
  padding-bottom: 140rpx;
}
.item {
  padding: 40rpx 20rpx;
  border-bottom: 10px solid #f5f5f5;
  background:#fff;
  .top {
    display: flex;
    font-weight: bold;
    font-size: 34rpx;
    .phone {
      margin-left: 60rpx;
    }
    .tag {
      display: flex;
      font-weight: normal;
      align-items: center;
      text {
        display: block;
        width: 60rpx;
        height: 34rpx;
        line-height: 34rpx;
        color: #ffffff;
        font-size: 20rpx;
        border-radius: 6rpx;
        text-align: center;
        margin-left: 30rpx;
        background-color: rgb(49, 145, 253);
      }
      .red {
        background-color: red;
      }
    }
  }
  .bottom {
    display: flex;
    margin-top: 20rpx;
    font-size: 28rpx;
    justify-content: space-between;
    color: #999999;
    .delete {
      margin-left: 30rpx;
    }
  }
  .icon-box {
    display: flex;
  }
}
.addSite {
  display: flex;
  justify-content: space-around;
  width: 600rpx;
  line-height: 100rpx;
  position: fixed;
  bottom: 30rpx;
  left: 80rpx;
  background-color: red;
  border-radius: 60rpx;
  font-size: 30rpx;
  .add {
    display: flex;
    align-items: center;
    color: #ffffff;
    .icon {
      margin-right: 10rpx;
    }
  }
}
</style>
